var app = getApp();
var queryUrl = app.globalData.queryUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    open: false,
    selectedTrade: '',
    version: '',
    sessionKey: '',
    winHeight: 0,
    auth: 1, //是否需要弹出授权浮层 
    user: {},
    userId: '',
    openId: '',
    shopId: '',
    auth: 0//是否需要弹出授权浮层
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    // that.setData({
    //   version: app.globalData.version
    // })
    // 获取系统信息 
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.animation = wx.createAnimation();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that = this;
    try {
      var openId = wx.getStorageSync('openId')
      var user = wx.getStorageSync('user')
      if (openId) {
        that.setData({
          openId: openId,
          userId: user.id
        })
      }
    } catch (e) { }
    wx.showLoading({
      title: '加载中'
    })
    //获取用户信息
    wx.request({
      url: queryUrl + 'my/userInfo',
      data: {
        userId: that.data.userId
      },
      success: function (res) {
        wx.hideLoading();
        //console.log(res.data)
        console.log(res)
        that.setData({
          shopId: res.data.user.shop_id,
          user: res.data.user
        })
        console.log(that.data.user)
        if (res.data.user.getAuth) {
          wx.login({
            success: function (res) {
              console.log(res)
              wx.request({
                url: queryUrl + 'auth/getOpenId',
                data: {
                  code: res.code
                },
                success: function (res1) {
                  console.log("getOpenId返回结果:" + (res1.data.sessionKey));
                  that.setData({
                    openId: res1.data.openId,
                    sessionKey: res1.data.sessionKey
                  })
                }
              });
            }
          })
        }
      }
    })
  },
  getUserInfo: function (e) {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      // console.log(e.detail.userInfo);
      var that = this;
      // 查看是否授权
      wx.getSetting({
        success: function (res) {
          //console.log(res);
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
            wx.getUserInfo({
              withCredentials: true,
              success: function (res1) {
                //console.log(res1)
                wx.request({
                  url: queryUrl + 'auth/login',
                  data: {
                    openId: that.data.openId,
                    nickname: res1.userInfo.nickName,
                    headImg: res1.userInfo.avatarUrl,
                    province: res1.userInfo.province,
                    city: res1.userInfo.city,
                    gender: res1.userInfo.gender,
                    encryptedData: res1.encryptedData,
                    iv: res1.iv,
                    sessionKey: that.data.sessionKey,
                  },
                  //method: 'post',
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res2) {
                    console.log(res2)
                    wx.hideToast();
                    wx.showToast({
                      title: '拉取成功',
                      icon: 'none'
                    });
                    console.log("用户信息更新成功");
                    //获取用户信息
                    wx.request({
                      url: queryUrl + 'my/userInfo',
                      data: {
                        // shopId:that.data.shopId
                        userId: that.data.userId
                      },
                      success: function (res3) {
                        console.log(res3)
                        that.setData({
                          auth: 0,
                          user: res3.data.user
                          // shopId: res.data.data.user.shopId,
                        })
                      }
                    })
                  }
                });
              }
            })
          }
        }
      })

    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '美助圈提醒您',
        content: '您点击了拒绝授权，将无法获取您的头像和昵称！',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goShop: function (e) {
    try {
      var user = wx.getStorageSync('user')
      if (user) {
        that.setData({
          shopId: user.shop_id
        })
      }
    } catch (e) { }
      wx.navigateTo({
        url: '../centerShop/shopcenter/shopcenter?shopId=' + shopId,
      })
  },
  goSub: function () {
    wx.navigateTo({
      url: '../usService/personal/personal',
    })
  },
  goMywallet: function () {
    wx.navigateTo({
      url: '../mywallet/wallet/wallet',
    })
  },
  goDis: function () {
    wx.navigateTo({
      url: '../districenter/mydistri/mydistri',
    })
  },
  gOrder: function () {
    wx.navigateTo({
      url: '../order/order',
    })
  },
  goSinfor: function () {
    wx.navigateTo({
      url: '../../setUp/selfInfor/selfInfor',
    })
  },
  goSauthen: function () {
    wx.navigateTo({
      url: '../../setUp/selfAuthen/selfAuthen',
    })
  },
  // 跳转个人简历模块
  goStay: function () {
    wx.navigateTo({
      url: '../order/order?currentData=1',
    })
  },
  goNot: function () {
    wx.navigateTo({
      url: '../order/order?currentData=2',
    })
  },
  goAlredy: function () {
    wx.navigateTo({
      url: '../order/order?currentData=3',
    })
  },
  goRefund: function () {
    wx.navigateTo({
      url: '../order/order?currentData=4',
    })
  },
  goAtt: function (e) {
    var uesrId = e.currentTarget.dataset.uesrid
    console.log(uesrId)
    wx.navigateTo({
      url: '../attention/attention?uesrId' + uesrId,
    })
  },
  goRcen: function () {
    wx.navigateTo({
      url: '../resumeCenter/index/index',
    })
  },
  goCard: function () {
    wx.navigateTo({
      url: '../memberCard/memberCard',
    })
  },
  tradeShow: function (e) {
    console.log(e);
    this.setData({
      isRuleTrue: true
    })
  },
  success: function (e) {
    var val = e.currentTarget.dataset.val
    this.setData({
      isRuleTrue: false,
      selectedTrade: val
    })
  },
  doCancel: function () {
    var that = this;
    that.setData({
      auth: 0
    })
  },
  doLogin: function () {
    var that = this;
    that.setData({
      auth: 1
    })
  },
  // 扫一扫
  scan: function () {
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        //扫描微信带参数二维码
        if (res.scanType == 'WX_CODE') {
          console.log('xiao cheng xu shou ye sao yi sao');
          console.log(res.path);
          var url = res.path;
          var arr = url.split('=');
          //console.log(arr);
          wx.showToast({
            title: '扫码成功',
            icon: 'loading',
            success: function () {
              that.setData({
                shopCode: arr[1],
                page: 0
              })
              //更新缓存shopCode
              try {
                wx.setStorageSync('shopCode', arr[1]);
                console.log('storage shopCode:', wx.getStorageSync('shopCode'));
              }
              catch (e) {
              }
              that.getIndexData();
            }
          });
        }
      }
    })
  }
})